package com.xbfinal.educms.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSON;
import com.xbfinal.commonUtils.JwtUtils;
import com.xbfinal.commonUtils.Result;
import com.xbfinal.commonUtils.ordervo.UcenterMemberOrder;
import com.xbfinal.educms.entity.UcenterMember;
import com.xbfinal.educms.entity.vo.RegisterVo;
import com.xbfinal.educms.service.UcenterMemberService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author xbfinal
 * @since 2023-02-28
 */
@Slf4j
@RestController
@RequestMapping("/educenter/member")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    /**
     * 登录
     * @return
     */
    @PostMapping("login")
    public Result login(@RequestBody UcenterMember ucenterMember){
        //调用方法实现登录 返回一个token
        String token = memberService.login(ucenterMember);
        return Result.ok().add("token",token);
    }

    /**
     * 注册
     */

    @ApiOperation("注册")
    @PostMapping("register")
    public Result register(@RequestBody RegisterVo registerVo){

        memberService.register(registerVo);
        return Result.ok();
    }
    /**
     * 根据token获取用户信息
     */
    @GetMapping("getMemberInfo")
    public Result getMemberInfo(HttpServletRequest request){
        //使用jwt工具栏方法，根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = memberService.getById(memberId);

        System.out.println("================");
        System.out.println(member);
        System.out.println("================");

        return Result.ok().add("userInfo",member);
    }

    /**
     * 根据用户id获取用户信息
     */
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable("id") String id){

        UcenterMember byId = memberService.getById(id);
        UcenterMemberOrder order = new UcenterMemberOrder();
        BeanUtils.copyProperties(byId,order);
        return order;
    }

    @ApiOperation("查询一天的注册人数")
    @GetMapping("countRegister/{day}")
    public Result countRegister(@PathVariable("day") String day){

        Integer count = memberService.countRegisterDay(day);
        return Result.ok().add("countRegister",count);
    }

}

